/*

Maelstrom - Maelstrom is an open source Actionscript 3.0 library for displaying
DirectDraw Surface (.DDS) image files.

http://code.google.com/p/maelstrom/

Copyright (c) 2010 - 2012 Bryan Dresselhaus, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/
package com.maelstrom.images.dds
{	
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	/**
	 * The DDSImageUtil parses dds images byte data.
	 * 
	 */	
	public class DDSImageUtil
	{	
		/**
		 * Decompresses a DXT1 compressed image.
		 * @param bytes The ByteArray containing image.
		 * @param bitmapData The BitmapData instance to write the pixels to.
		 * @param height The height of the image.
		 * @param width The width of the image.
		 * 
		 */
		
		// The following code is optimized for fast execution.
		
		public static function parseDXT1(bytes:ByteArray, bitmapData:BitmapData, height:int, width:int):void
		{
			var x:int = 0;
			var y:int = 0;
			
			var bits:Vector.<int> = new Vector.<int>(3);
			var colors:Vector.<uint> = new Vector.<uint>(3);
			var pixels:Vector.<uint> = new Vector.<uint>(height * width);
			
			// Color Variables
			
			var c0:uint = 0, c1:uint = 0;
			
			var redColor:int = 0, greenColor:int = 0, blueColor:int = 0;
			var redColor1:int = 0, greenColor1:int = 0, blueColor1:int = 0;
			
			// End Color Variables
			
			while (y < height)
			{
				// Colors
				
				c0 = bytes[bytes.position] | bytes[bytes.position+1] << 8;
				
				redColor = (((c0 >> 11) & 0x1F) << 3) | (((c0 >> 11) & 0x1F) >> 2);
				greenColor = (((c0 >> 5) & 0x3F) << 2) | (((c0 >> 5) & 0x3F) >> 4);
				blueColor = ((c0 & 0x1F) << 3) | ((c0 & 0x1F) >> 2);
				
				colors[0] = redColor << 16 | greenColor << 8 | blueColor;
				
				c1 = bytes[bytes.position+2] | bytes[bytes.position+3] << 8;
				
				redColor1 = (((c1 >> 11) & 0x1F) << 3) | (((c1 >> 11) & 0x1F) >> 2);
				greenColor1 = (((c1 >> 5) & 0x3F) << 2) | (((c1 >> 5) & 0x3F) >> 4);
				blueColor1 = ((c1 & 0x1F) << 3) | ((c1 & 0x1F) >> 2);
				
				colors[1] = redColor1 << 16 | greenColor1 << 8 | blueColor1;
				
				colors[2] = (((2 * redColor) + redColor1) / 3) << 16 | (((2 * greenColor) + greenColor1) / 3) << 8 | ((2 * blueColor) + blueColor1) / 3;
				
				colors[3] = ((redColor + (2 * redColor1)) / 3) << 16 | ((greenColor + (2 * greenColor1)) / 3) << 8 | (blueColor + (2 * blueColor1)) / 3;
				
				// End Colors
				
				// Bits
				
				bits[0] = bytes[bytes.position+4];
				bits[1] = bytes[bytes.position+5];
				bits[2] = bytes[bytes.position+6];
				bits[3] = bytes[bytes.position+7];
				
				// End Bits
				
				bytes.position += 8;
				
				// Pixel Block
				
				pixels[(y * width) + x] = 0xFF << 24 | colors[(bits[0] >> 0) & 3];
				pixels[(y * width) + x+1] = 0xFF << 24 | colors[(bits[0] >> 2) & 3];
				pixels[(y * width) + x+2] = 0xFF << 24 | colors[(bits[0] >> 4) & 3];
				pixels[(y * width) + x+3] = 0xFF << 24 | colors[(bits[0] >> 6) & 3];
				
				// 1
				
				pixels[((y+1) * width) + x] = 0xFF << 24 | colors[(bits[1] >> 0) & 3];
				pixels[((y+1) * width) + x+1] = 0xFF << 24 | colors[(bits[1] >> 2) & 3];
				pixels[((y+1) * width) + x+2] = 0xFF << 24 | colors[(bits[1] >> 4) & 3];
				pixels[((y+1) * width) + x+3] = 0xFF << 24 | colors[(bits[1] >> 6) & 3];
				
				// 2
				
				pixels[((y+2) * width) + x] = 0xFF << 24 | colors[(bits[2] >> 0) & 3];
				pixels[((y+2) * width) + x+1] = 0xFF << 24 | colors[(bits[2] >> 2) & 3];
				pixels[((y+2) * width) + x+2] = 0xFF << 24 | colors[(bits[2] >> 4) & 3];
				pixels[((y+2) * width) + x+3] = 0xFF << 24 | colors[(bits[2] >> 6) & 3];
				
				// 3
				
				pixels[((y+3) * width) + x] = 0xFF << 24 | colors[(bits[3] >> 0) & 3];
				pixels[((y+3) * width) + x+1] = 0xFF << 24 | colors[(bits[3] >> 2) & 3];
				pixels[((y+3) * width) + x+2] = 0xFF << 24 | colors[(bits[3] >> 4) & 3];
				pixels[((y+3) * width) + x+3] = 0xFF << 24 | colors[(bits[3] >> 6) & 3];
				
				// End Pixel Block
				
				x+=4;
					
				if (x == width)
				{
					x = 0;
					y += 4;
				}
			}
			
			bitmapData.setVector(new Rectangle(0, 0, width, height), pixels);
		}
		
		/**
		 * Decompresses a DXT5 compressed image.
		 * @param bytes The ByteArray containing image.
		 * @param bitmapData The BitmapData instance to write the pixels to.
		 * @param height The height of the image.
		 * @param width The width of the image.
		 * 
		 */
		
		// The following code is optimized for fast execution.
		
		public static function parseDXT5(bytes:ByteArray, bitmapData:BitmapData, height:int, width:int):void
		{
			var x:int = 0;
			var y:int = 0;
			
			var alphas:Vector.<uint> = new Vector.<uint>(7);
			var alphaBits:Vector.<int> = new Vector.<int>(15);
			var bits:Vector.<int> = new Vector.<int>(3);
			var colors:Vector.<uint> = new Vector.<uint>(3);
			var pixels:Vector.<uint> = new Vector.<uint>(height * width);
			
			var alphaByte1:uint = 0, alphaByte2:uint = 0;
			
			var a0:int = 0, a1:int = 0;
			
			// Color Variables
			
			var c0:uint = 0, c1:uint = 0;
			
			var redColor:int = 0, greenColor:int = 0, blueColor:int = 0;
			var redColor1:int = 0, greenColor1:int = 0, blueColor1:int = 0;
			
			// End Color Variables
			
			while (y < height)
			{
				// Alpha Values
				
				a0 = bytes.readByte();
				a1 = bytes.readByte();
				
				alphas[0] = a0;
				alphas[1] = a1;
				
				if (a0 < a1)
				{
					alphas[2] = ((4 * a0) + a1) / 5;
					alphas[3] = ((3 * a0) + (2 * a1)) / 5;
					alphas[4] = ((2 * a0) + (3 * a1)) / 5;
					alphas[5] = ((1 * a0) + (4 * a1)) / 5;
					alphas[6] = 0;
					alphas[7] = 255;
				}
				else
				{
					alphas[3] = ((6 * a0) + a1) / 7
					alphas[4] = ((5 * a0) + (2 * a1)) / 7
					alphas[5] = ((4 * a0) + (3 * a1)) / 7
					alphas[6] = ((3 * a0) + (4 * a1)) / 7
					alphas[7] = ((2 * a0) + (5 * a1)) / 7
					alphas[8] = ((1 * a0) + (6 * a1)) / 7
				}
				
				// End Alpha Values
				
				// Alpha Bits
				
				alphaByte1 = bytes[bytes.position+2] << 16 | bytes[bytes.position+1] << 8 | bytes[bytes.position];
				alphaByte2 = bytes[bytes.position+5] << 16 | bytes[bytes.position+4] << 8 | bytes[bytes.position+3];
				
				alphaBits[0] = alphaByte1 >> 0 & 7;
				alphaBits[1] = alphaByte1 >> 3 & 7;
				alphaBits[2] = alphaByte1 >> 6 & 7;
				alphaBits[3] = alphaByte1 >> 9 & 7;
				alphaBits[4] = alphaByte1 >> 12 & 7;
				alphaBits[5] = alphaByte1 >> 15 & 7;
				alphaBits[6] = alphaByte1 >> 18 & 7;
				alphaBits[7] = alphaByte1 >> 21 & 7;
				alphaBits[8] = alphaByte2 >> 0 & 7;
				alphaBits[9] = alphaByte2 >> 3 & 7;
				alphaBits[10] = alphaByte2 >> 6 & 7;
				alphaBits[11] = alphaByte2 >> 9 & 7;
				alphaBits[12] = alphaByte2 >> 12 & 7;
				alphaBits[13] = alphaByte2 >> 15 & 7;
				alphaBits[14] = alphaByte2 >> 18 & 7;
				alphaBits[15] = alphaByte2 >> 21 & 7;
				
				// End Alpha Bits
				
				// Colors
				
				c0 = bytes[bytes.position+6] | bytes[bytes.position+7] << 8;
				
				redColor = (((c0 >> 11) & 0x1F) << 3) | (((c0 >> 11) & 0x1F) >> 2);
				greenColor = (((c0 >> 5) & 0x3F) << 2) | (((c0 >> 5) & 0x3F) >> 4);
				blueColor = ((c0 & 0x1F) << 3) | ((c0 & 0x1F) >> 2);
				
				colors[0] = redColor << 16 | greenColor << 8 | blueColor;
				
				c1 = bytes[bytes.position+8] | bytes[bytes.position+9] << 8;
				
				redColor1 = (((c1 >> 11) & 0x1F) << 3) | (((c1 >> 11) & 0x1F) >> 2);
				greenColor1 = (((c1 >> 5) & 0x3F) << 2) | (((c1 >> 5) & 0x3F) >> 4);
				blueColor1 = ((c1 & 0x1F) << 3) | ((c1 & 0x1F) >> 2);
				
				colors[1] = redColor1 << 16 | greenColor1 << 8 | blueColor1;
				
				colors[2] = (((2 * redColor) + redColor1) / 3) << 16 | (((2 * greenColor) + greenColor1) / 3) << 8 | ((2 * blueColor) + blueColor1) / 3;
				
				colors[3] = ((redColor + (2 * redColor1)) / 3) << 16 | ((greenColor + (2 * greenColor1)) / 3) << 8 | (blueColor + (2 * blueColor1)) / 3;
				
				// End Colors
				
				bits[0] = bytes[bytes.position+10];
				bits[1] = bytes[bytes.position+11];
				bits[2] = bytes[bytes.position+12];
				bits[3] = bytes[bytes.position+13];
				
				// Pixel Block
				
				pixels[(y * width) + x] = alphas[alphaBits[0]] << 24 | colors[(bits[0] >> 0) & 3];
				pixels[(y * width) + x+1] = alphas[alphaBits[1]] << 24 | colors[(bits[0] >> 2) & 3];
				pixels[(y * width) + x+2] = alphas[alphaBits[2]] << 24 | colors[(bits[0] >> 4) & 3];
				pixels[(y * width) + x+3] = alphas[alphaBits[3]] << 24 | colors[(bits[0] >> 6) & 3];
				
				// 1
				
				pixels[((y+1) * width) + x] = alphas[alphaBits[4]] << 24 | colors[(bits[1] >> 0) & 3];
				pixels[((y+1) * width) + x+1] = alphas[alphaBits[5]] << 24 | colors[(bits[1] >> 2) & 3];
				pixels[((y+1) * width) + x+2] = alphas[alphaBits[6]] << 24 | colors[(bits[1] >> 4) & 3];
				pixels[((y+1) * width) + x+3] = alphas[alphaBits[7]] << 24 | colors[(bits[1] >> 6) & 3];
				
				// 2
				
				pixels[((y+2) * width) + x] = alphas[alphaBits[8]] << 24 | colors[(bits[2] >> 0) & 3];
				pixels[((y+2) * width) + x+1] = alphas[alphaBits[9]] << 24 | colors[(bits[2] >> 2) & 3];
				pixels[((y+2) * width) + x+2] = alphas[alphaBits[10]] << 24 | colors[(bits[2] >> 4) & 3];
				pixels[((y+2) * width) + x+3] = alphas[alphaBits[11]] << 24 | colors[(bits[2] >> 6) & 3];
				
				// 3
				
				pixels[((y+3) * width) + x] = alphas[alphaBits[12]] << 24 | colors[(bits[3] >> 0) & 3];
				pixels[((y+3) * width) + x+1] = alphas[alphaBits[13]] << 24 | colors[(bits[3] >> 2) & 3];
				pixels[((y+3) * width) + x+2] = alphas[alphaBits[14]] << 24 | colors[(bits[3] >> 4) & 3];
				pixels[((y+3) * width) + x+3] = alphas[alphaBits[15]] << 24 | colors[(bits[3] >> 6) & 3];

				// End Pixel Block
				
				bytes.position += 14;
				
				x+=4;
					
				if (x == width)
				{
					x = 0;
					y += 4;
				}
			}
			
			bitmapData.setVector(new Rectangle(0, 0, width, height), pixels);
		}

	}
}